package com.bankLogisticsSys.util;

import redis.clients.jedis.Jedis;

public class Redis {

	private static Jedis jedis;

	public static void main(String[] args) {
		// 连接本地的 Redis 服务
		try {
			Jedis jedis = new Jedis("localhost");
			System.out.println("Connection to server sucessfully");
			// 查看服务是否运行
			// redis string
			jedis.set("test", "smartian");
			// System.out.println("Server is running: "+jedis.ping());
			System.out.println("test jedis get key:" + jedis.get("test"));
			// redis list
			jedis.lpush("hotSearch", "Java Web");
			jedis.lpush("hotSearch", "C++");
			jedis.lpush("hotSearch", "择天记");
			jedis.lpush("hotSearch", "刘明");

			hashSetAction("test", 1);
			hashSetTrackingID("test",123);
			
		} catch (Exception e) {
			System.out.println("redis connection failed");
		}
	}

	//缓存当前环节编号
	public static void hashSetAction(String openid, int aciton) {
		try {
			jedis = new Jedis("localhost");
			System.out.println("Connection to server sucessfully");
			// 查看服务是否运行
			jedis.hset(openid, "action", String.valueOf(aciton));
		} catch (Exception e) {
			System.out.println("redis connection failed");
		}
	}
	
	public static int hashGetAction(String openid) {

		int action = 0;
		try {
			jedis = new Jedis("localhost");
			System.out.println("Connection to server sucessfully");
			// 查看服务是否运行
			if (jedis.hget(openid, "action") != null) {
				action = Integer.parseInt(jedis.hget(openid, "action"));
				System.out.println("action is :" + action);
			}
		} catch (Exception e) {
			System.out.println("redis connection failed");
		}
		return action;
	}

	public static void hashSetTrackingID(String openid, int trackingID) {
		try {
			jedis = new Jedis("localhost");
			System.out.println("Connection to server sucessfully");
			// 查看服务是否运行
			jedis.hset(openid, "trackingID", String.valueOf(trackingID));
		} catch (Exception e) {
			System.out.println("redis connection failed");
		}
	}

	public static int hashGetTrackingID(String openid) {

		int trackingID = 0;
		try {
			jedis = new Jedis("localhost");
			System.out.println("Connection to server sucessfully");
			// 查看服务是否运行
			if (jedis.hget(openid, "trackingID") != null) {
				trackingID = Integer.parseInt(jedis.hget(openid, "trackingID"));
				System.out.println("trackingID is :" + trackingID);
			}
		} catch (Exception e) {
			System.out.println("redis connection failed");
		}
		return trackingID;
	}

}
